//
//  GdlistTableViewCell.m
//  Contractor
//
//  Created by 金鑫 on 16/5/19.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "GdlistTableViewCell.h"
#import "CONST.h"

@implementation GdlistTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
        self.statusLabel = [self creatlabel:14.0f color:[UIColor blueColor]] ;
        
        self.nameLabel = [self creatlabel:14.0f color:[UIColor blackColor]] ;
        
        self.addressLabel = [self creatlabel:14.0f color:[UIColor grayColor]] ;
        
        self.amountLabel = [self creatlabel:14.0f color:[UIColor redColor]] ;
        
        self.timeLabel = [self creatlabel:14.0f color:[UIColor lightGrayColor]] ;
       
        UIImageView *imageView = [[UIImageView alloc] init] ;
        [self.contentView addSubview:imageView] ;
        self.lineView = imageView ;
        
        UIImageView *rightImageview= [[UIImageView alloc] init] ;
        [self.contentView addSubview:rightImageview] ;
        self.rightImage = rightImageview ;
        
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
//        [self.contentView addSubview:btn] ;
//        self.statusBtn = btn ;
    }
    return self ;
}

//创建label
- (UILabel *)creatlabel:(CGFloat) size color:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] init] ;
    label.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:size] ;
    label.backgroundColor = [UIColor clearColor] ;
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = color ;
    [self.contentView addSubview:label] ;
    return label ;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"cellID" ;
    GdlistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier] ;
    if (!cell) {
        cell = [[GdlistTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    return cell ;
}

- (void)config:(SheetModel *)model
{
    self.backgroundColor = [UIColor whiteColor] ;
    if ([model.gdstatusid integerValue] == 9 || [model.gdstatusid integerValue] == 21 || [model.gdstatusid integerValue] == 11) {
        self.statusLabel.text = @"";
    }
    else{
        self.statusLabel.text = model.gdstatus ;
    }
    self.nameLabel.text = model.consumername ;
    self.amountLabel.text = [NSString stringWithFormat:@"￥%@",model.serviceamount] ;
//    NSString *address = model.gdserverlocate ;
//    address = [self address:address] ;
    NSString *location = [NSString stringWithFormat:@"%@ %@",model.gdserverlocate,model.consumeraddress] ;
    self.addressLabel.text = location ;
    
    if ([model.gdstatusid integerValue] > 4) {
        NSString *begin = [model.servicedatetimebegin substringWithRange:NSMakeRange(model.servicedatetimebegin.length - 8, 5)] ;
        NSString *end = [model.servicedatetimeend substringWithRange:NSMakeRange(model.servicedatetimebegin.length - 8, 5)] ;
        NSString *day = [model.servicedatetimebegin substringWithRange:NSMakeRange(5, 5)] ;
        NSString *time = [NSString stringWithFormat:@"%@ %@~%@",day,begin,end] ;
        self.timeLabel.text = time ;
    }
        
    [self.lineView setImage:[UIImage imageNamed:@"press_bg.jpg"]] ;
    
   // [self.statusBtn setTitle:@"" forState:<#(UIControlState)#>]
    
    if ([model.gdstatusid integerValue] == 9) {
        [self.statusBtn removeFromSuperview] ;
        
        [self.rightImage setImage:[UIImage imageNamed:@"order_list_already_finished.png"]] ;
        self.rightImage.frame = CGRectMake(__kScreenWidth - 80, 10, 60, 48) ;
        [self.contentView addSubview:self.rightImage] ;
    }
    else if ([model.gdstatusid integerValue] == 10 || [model.gdstatusid integerValue] == 11) {
        [self.statusBtn removeFromSuperview] ;
        
        [self.rightImage setImage:[UIImage imageNamed:@"order_list_abnormaity_order.png"]] ;
        self.rightImage.frame = CGRectMake(__kScreenWidth - 80, 10, 60, 48) ;
        [self.contentView addSubview:self.rightImage] ;
    }
    else if ([model.gdstatusid integerValue] == 21) {
        [self.statusBtn removeFromSuperview] ;
        
        [self.rightImage setImage:[UIImage imageNamed:@"order_list_check_order.png"]] ;
        self.rightImage.frame = CGRectMake(__kScreenWidth - 80, 10, 60, 48) ;
        [self.contentView addSubview:self.rightImage] ;
    }
    else{
        [self.rightImage removeFromSuperview] ;
        [self.contentView addSubview:self.statusBtn] ;
    }
//    if ([model.gdstatusid integerValue] == 5) {
//        [self.contentView addSubview:self.statusBtn] ;
//    }
//    if ([model.gdstatusid integerValue] == 3) {
//        [self.statusBtn setTitle:@"接单" forState:UIControlStateNormal] ;
//        self.statusBtn.frame = CGRectMake(__kScreenWidth*2/3 + 10, 15, __kScreenWidth/3 - 20, 45) ;
//        self.statusBtn.backgroundColor = [UIColor blueColor] ;
//        self.statusBtn.layer.cornerRadius = 5.0f ;
//        
//        [self.contentView addSubview:self.statusBtn] ;
//    }
//    else if ([model.gdstatusid integerValue] == 5)
//    {
//        [self.statusBtn setTitle:@"上门安装" forState:UIControlStateNormal] ;
//        self.statusBtn.frame = CGRectMake(__kScreenWidth*2/3 + 10, 15, __kScreenWidth/3 - 20, 45) ;
//        self.statusBtn.backgroundColor = [UIColor blueColor] ;
//        self.statusBtn.layer.cornerRadius = 5.0f ;
//        
//        [self.contentView addSubview:self.statusBtn] ;
//    }
//    else if ([model.gdstatusid integerValue] == 6)
//    {
//        [self.statusBtn setTitle:@"上门验证" forState:UIControlStateNormal] ;
//        self.statusBtn.frame = CGRectMake(__kScreenWidth*2/3 + 10, 15, __kScreenWidth/3 - 20, 45) ;
//        self.statusBtn.backgroundColor = [UIColor blueColor] ;
//        self.statusBtn.layer.cornerRadius = 5.0f ;
//        
//        [self.contentView addSubview:self.statusBtn] ;
//    }
//
//    else
//    {
//        [self.statusBtn removeFromSuperview] ;
//    }

    
    [self setSubviewFrame] ;
    
}

- (NSString *)address:(NSString *)str
{
    for (int i = 0; i < str.length; i++) {
        NSString *temp = [str substringWithRange:NSMakeRange(i, 1)] ;
        NSLog(@"temp:%@",temp) ;
        NSLog(@"i:%d",i) ;
        if ([temp isEqualToString:@"|"]) {
            str = [str substringWithRange:NSMakeRange(i+1, str.length-i-1)] ;
            NSLog(@"str:%@",str) ;
            i = -1 ;
        }
    }
    return str ;
}

- (void)setSubviewFrame
{
    CGSize statuslength = [self lengthwithLabel:self.statusLabel withsize:14.0f] ;
    self.statusLabel.frame = CGRectMake(10, 10, statuslength.width, 16.0f) ;
    CGSize namelenth = [self lengthwithLabel:self.nameLabel withsize:14.0f] ;
    self.nameLabel.frame = CGRectMake(10, self.statusLabel.frame.origin.y + self.statusLabel.frame.size.height + 5, namelenth.width, 16.0f) ;
    //CGSize addresslenght = [self lengthwithLabel:self.addressLabel withsize:14.0f] ;
    self.addressLabel.textAlignment = NSTextAlignmentLeft ;
    self.addressLabel.frame = CGRectMake(10, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height + 5, __kScreenWidth - 120, 16.0f) ;
    self.lineView.frame = CGRectMake(0, 75, __kScreenWidth, 5) ;
    
    CGSize timelength = [self lengthwithLabel:self.timeLabel withsize:14.0f] ;
    
    self.timeLabel.frame = CGRectMake(__kScreenWidth*2/3 - 10 - timelength.width, self.nameLabel.frame.origin.y, timelength.width, 16.0f) ;
    CGSize amountlength = [self lengthwithLabel:self.amountLabel withsize:14.0f] ;
    self.amountLabel.frame = CGRectMake(__kScreenWidth*2/3 - 10 - amountlength.width, self.statusLabel.frame.origin.y, amountlength.width, 16.0f) ;
    
    
}

- (CGSize)lengthwithLabel:(UILabel *)lable withsize:(CGFloat) size
{
    NSDictionary *attribute1 = @{NSFontAttributeName: [UIFont fontWithName:@"AvenirLTStd-Light" size:size]};
    CGSize DescSize = [lable.text boundingRectWithSize:CGSizeMake(MAXFLOAT, size) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    return DescSize ;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
